#!/usr/bin/python3
import argparse
from tools import ObsTools


class Cmd:
    """
    命令行
    """

    parser = argparse.ArgumentParser(description="obs package")
    subparsers = parser.add_subparsers(help="obs package")

    def __init__(self) -> None:
        self.tools = ObsTools()

    def run(self):
        """
        运行命令行
        """
        for cmd in self._register_cmd():
            cmd()
        try:
            args = self.parser.parse_args()
            args.func(args)
        except Exception as error:
            print("The command execution failed due to:{}".format(error))

    def _build(self, cmd):
        self.tools.build(package=cmd.package, project=cmd.project)

    def _create(self, cmd):
        self.tools.add_package(
            package=cmd.package, project=cmd.project, path=cmd.path, build=cmd.build
        )

    def _depend(self, cmd):
        self.tools.depend(
            project=cmd.project,
            file=cmd.file,
            rpm=cmd.rpm,
            unresolvable=cmd.unresolvable,
        )

    def _install(self, cmd):
        self.tools.install(project=cmd.project, package=cmd.package, csv_file=cmd.file)

    def _extract(self, cmd):
        self.tools.extract_json(
            file=cmd.file,
            packages=cmd.package,
            build=cmd.build,
            install=cmd.install,
            out_file=cmd.out,
        )

    def _add_arg_package(self, parse, cmd):
        parse.add_argument(cmd, help="The RPM package name", default=None)

    def _add_arg_project(self, parse, project):
        parse.add_argument(project, help="Project name in OBS", default=None)

    def _register_cmd(self):
        def build():
            parse = self.subparsers.add_parser(
                "build",
                help="Triggers compilation of the entire OBS project or a single RPM",
            )
            self._add_arg_package(parse, "-package")
            self._add_arg_project(parse, "-project")
            parse.set_defaults(func=self._build)

        def create():
            parse = self.subparsers.add_parser(
                "create",
                help="Create an RPM package and upload the associated source files",
            )
            self._add_arg_package(parse, "package")
            self._add_arg_project(parse, "-project")
            parse.add_argument(
                "-path", help="A single file or a path for storing a file", default=None
            )
            parse.add_argument(
                "--build",
                help="Whether to trigger OBS compilation，The default is not to compile",
                default=False,
                action="store_true",
            )
            parse.set_defaults(func=self._create)

        def depend():
            parse = self.subparsers.add_parser(
                "depend", help="Unresolvable dependency in the project"
            )
            self._add_arg_project(parse, "-project")
            parse.add_argument(
                "-file", help="Files to be operated locally in batches", default=None
            )
            # parse.add_argument("-packages", nargs="*", help="", default=[])
            parse.add_argument("-rpm", help="", default=None)
            parse.add_argument(
                "--unresolvable",
                help="Whether to obtain packages in the unresolvable state",
                default=False,
                action="store_true",
            )
            parse.set_defaults(func=self._depend)

        def install():
            parse = self.subparsers.add_parser(
                "install", help="Binary package installation verification"
            )
            self._add_arg_project(parse, "project")
            self._add_arg_package(parse, "-package")
            parse.add_argument("-file", help="The output CSV file", default=None)
            parse.set_defaults(func=self._install)

        def extract():
            parse = self.subparsers.add_parser(
                "extract", help="Extract the specific content in the JSON result"
            )
            parse.add_argument("file", help="Json file")
            parse.add_argument(
                "-package", nargs="*", help="software package", default=[]
            )
            group_parse = parse.add_mutually_exclusive_group(required=True)
            group_parse.add_argument(
                "--build",
                help="Compiling Dependent Components",
                default=False,
                action="store_true",
            )
            group_parse.add_argument(
                "--install",
                help="Installing Dependent Components",
                default=False,
                action="store_true",
            )
            parse.add_argument("-out", help="Output file", default=None)
            parse.set_defaults(func=self._extract)

        return [build, create, depend, install, extract]


if __name__ == "__main__":
    _cmd = Cmd()
    _cmd.run()
